using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using rendertexture3dxna;

namespace _3dtextxna
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private Texture2D texture;

        BasicEffect basicEffect;
        Matrix View, Projection;
        TexturedQuad sprite;


        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";


            graphics.PreferredBackBufferWidth = 512;
            graphics.PreferredBackBufferHeight = 512;
        }

        private SpriteFont font;
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            SpriteFontHelper.Initialize(GraphicsDevice);


            font = Content.Load<SpriteFont>("SpriteFont1");
            texture = SpriteFontHelper.SpriteFontTextToTexture(font, "Hello World");

            View = Matrix.CreateLookAt(new Vector3(0, 0, 500), Vector3.Zero,
                Vector3.Up);
            //Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4, 4.0f / 3.0f, 1, 1000);

            Projection = Matrix.CreateOrthographicOffCenter(-256, 256, -256, 256, 1, 1000);

            sprite = new TexturedQuad(new Vector3(0, 0, 0), Vector3.Backward, Vector3.Up, new Rectangle(0, 0, texture.Width, texture.Height), new Vector2(texture.Width, texture.Height), new Vector2(texture.Width, texture.Height));

            basicEffect = new BasicEffect(GraphicsDevice);
            //basicEffect.EnableDefaultLighting();

            basicEffect.World = Matrix.Identity;
            basicEffect.View = View;
            basicEffect.Projection = Projection;
            basicEffect.TextureEnabled = true;
            basicEffect.Texture = texture;

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            float rotation = MathHelper.WrapAngle((float) gameTime.TotalGameTime.TotalMilliseconds/600);


            if (rotation < -MathHelper.PiOver2 || rotation > MathHelper.PiOver2)
                rotation = MathHelper.Pi + rotation;


            basicEffect.World = Matrix.CreateRotationY(rotation);

            texture = SpriteFontHelper.SpriteFontTextToTexture(font, "" +rotation);

            basicEffect.Texture = texture;

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(texture, Vector2.Zero, Color.White);
            

            
            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                GraphicsDevice.DrawUserIndexedPrimitives
                    <VertexPositionNormalTexture>(
                    PrimitiveType.TriangleList,
                    sprite.Vertices, 0, 4,
                    sprite.Indexes, 0, 2);
            }

            spriteBatch.End();
            
            base.Draw(gameTime);
        }
    }
}
